﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Brjostagjof.Models
{
    public class BrjostagjofRepository : IBrjostagjofRepository
    {
        BrjostagjofDBDataContext m_db = new BrjostagjofDBDataContext();

        #region Fréttagreinar

        public IEnumerable<NewsArticle> GetAllNews()
        {
            var result = from news in m_db.NewsArticles
                         where news.IsDeleted == false
                         orderby news.Date descending
                         select news;
            return (result);
        }

        public IEnumerable<NewsArticle> GetAllDeletedNews()
        {
            var result = from news in m_db.NewsArticles
                         where news.IsDeleted == true
                         orderby news.Date descending
                         select news;
            return (result);
        }

        public void SaveNewsArticle(NewsArticle newNewsArticle)
        {
            m_db.NewsArticles.InsertOnSubmit( newNewsArticle );
            m_db.SubmitChanges();
        }

       public void UpdateNewsArticle( int id, NewsArticle changedNewsArticle )
        {
            var toChange = (from na in m_db.NewsArticles
                          where na.ID == id
                          select na).SingleOrDefault();
            toChange.Title = changedNewsArticle.Title;
            toChange.Body = changedNewsArticle.Body;
            m_db.SubmitChanges();
           
        }

        public void DeleteNewsArticle(int id)
        { 
            var toDelete = (from na in m_db.NewsArticles
                            where na.ID == id
                            select na).SingleOrDefault();
            toDelete.IsDeleted = true;
            m_db.SubmitChanges();
        }
        #endregion

        #region Greinar

        public IEnumerable<Article> GetAllArticles()
        {
            var result = from articles in m_db.Articles
                         where articles.IsDeleted == false
                         orderby articles.Date descending
                         select articles;
            return (result);
        }

        public IEnumerable<Article> GetAllDeletedArticles()
        {
            var result = from articles in m_db.Articles
                         where articles.IsDeleted == true
                         orderby articles.Date descending
                         select articles;
            return (result);
        }

        public Article GetArticleById(int id)
        {
            Article result = (from na in m_db.Articles
                              where na.ID == id
                              select na).SingleOrDefault();
            if (result == null)
            {
                return null;
            }
            else
            {
                result.Count += 1;
                m_db.SubmitChanges();
                return result;
            }
        }

        public void SaveArticle(Article newArticle)
        {
            m_db.Articles.InsertOnSubmit(newArticle);
            m_db.SubmitChanges();
        }

        public void UpdateArticle(int id, Article changedArticle)
        {
            var toChange = (from na in m_db.Articles
                            where na.ID == id
                            select na).SingleOrDefault();
            toChange.Title = changedArticle.Title;
            toChange.Body = changedArticle.Body;
            toChange.Category = changedArticle.Category;
            m_db.SubmitChanges();
        }

        public void DeleteArticle(int id)
        {
            var toDelete = (from a in m_db.Articles
                            where a.ID == id
                            select a).SingleOrDefault();
            toDelete.IsDeleted = true;
            m_db.SubmitChanges();
        }

        #endregion

        #region Auglýsingar

        public IEnumerable<Ad> GetAllAds()
        {
            var result = from ads in m_db.Ads
                         select ads;
            return (result);
        }

        public void SaveAd(Ad newAd)
        {
            m_db.Ads.InsertOnSubmit(newAd);
            m_db.SubmitChanges();
        }
        public Ad GetNewestAd()
        {
            var result = (from a in m_db.Ads
                          orderby a.Date descending
                          select a).FirstOrDefault();
            return (result);
        }
        #endregion

        #region Flokkar

        public IEnumerable<Category> GetAllCategories()
        {
            var result = from c in m_db.Categories
                         orderby c.Category1 ascending
                         select c;

            
            return (result);
        }
        public void SaveCategory(Category categoryToSave)
        {
            m_db.Categories.InsertOnSubmit(categoryToSave);
            m_db.SubmitChanges();
        }
        #endregion

        #region Algengar Spurningar

            public IEnumerable<CommonQuestion> GetAllCommonQuestions()
            {
                var result = from cq in m_db.CommonQuestions
                             orderby cq.Date descending
                             select cq;
                return (result);
            }

            public void SaveCommonQuestion(CommonQuestion faqToSave)
            {
                faqToSave.Date = DateTime.Now;
                faqToSave.Draft = false;
                m_db.CommonQuestions.InsertOnSubmit(faqToSave);
                m_db.SubmitChanges();
                
            }
        #endregion
            
        #region Myndir
            public void SaveImage( Image imgToSave )
            {
                m_db.Images.InsertOnSubmit( imgToSave );
                m_db.SubmitChanges();
            }

           public IEnumerable<Image> GetAllImages()
            {
               var result = from img in m_db.Images
                            select img;
                return (result);
            }

           public IEnumerable<Image> GetImageByUser(Guid id)
           {
               var result = from img in m_db.Images
                            where img.UploaderID1 == id
                            select img;
               return (result);
                          
           }

           public IEnumerable<Image> GetImageByChild( Guid parentID, int childID)
           {
               var result = from img in m_db.Images 
                            join child in m_db.Childs on img.UploaderID1 equals child.ParentID
                            where img.UploaderID1 == parentID && child.ID==childID && img.ChildID == childID
                 select img;
               return (result);

           }


        #endregion

        #region Myndbönd
           public void SaveVideo(Video vidToSave)
           {
               m_db.Videos.InsertOnSubmit(vidToSave);
               m_db.SubmitChanges();
           }

           public IEnumerable<Video> GetAllVideos()
           {

               var result = from vid in m_db.Videos
                            orderby vid.VideoTitle ascending
                            select vid;
               return (result);
           }

           public Video GetVideoById(int? id)
           {
               var result = (from vid in m_db.Videos
                             where vid.ID == id.Value
                             orderby vid.VideoTitle ascending
                             select vid).SingleOrDefault();
               return (result);
           }
        #endregion

        #region YouTube
            public void SaveYouTube(YouTube ytToSave)
           {
               m_db.YouTubes.InsertOnSubmit(ytToSave);
               m_db.SubmitChanges();
           }

           public IEnumerable<YouTube> GetAllYT()
           {

               var result = from vid in m_db.YouTubes
                            orderby vid.YTTitle ascending
                            select vid;
               return (result);
           }

           public YouTube GetYTById(int? id)
           {
               YouTube result = (from yturl in m_db.YouTubes
                             where yturl.ID == id.Value
                             orderby yturl.YTTitle ascending
                             select yturl).SingleOrDefault();
               return (result);
           }
        #endregion

        #region Users
           public void SaveUserInfo( RegisterViewModel newUserInfo )
           {
               var toChange = (from userInfo in m_db.aspnet_Memberships
                               where userInfo.Email == newUserInfo.registerModel.Email
                               select userInfo).SingleOrDefault();

               toChange.FullName = newUserInfo.userInfo.FullName;
               toChange.Subscriber = newUserInfo.userInfo.Subscriber;
               toChange.fagadili = newUserInfo.userInfo.fagadili;
               m_db.SubmitChanges();
           }

           public IEnumerable<aspnet_User> GetAllUsers()
           {
               var result = from user in m_db.aspnet_Users
                            select user;
               return result;
           }

           public IEnumerable<aspnet_Membership> GetAllSubscribers()
           {
               var result = from user in m_db.aspnet_Users
                            from userInfo in m_db.aspnet_Memberships
                            where user.UserId == userInfo.UserId
                            && userInfo.Subscriber == true
                            select userInfo;
               return result;
           }

           public IEnumerable<aspnet_Membership> GetAllAdmins()
           { 
               var admin = new Guid("3ed83570-6328-4f29-b8bc-ee0b867cfed7");
               var result = from u in (from userRoles in m_db.aspnet_UsersInRoles
                                       where userRoles.RoleId == admin
                                       select userRoles)
                            from m in m_db.aspnet_Memberships
                            where u.UserId == m.UserId
                            select m;
               return result;
           }

            public String GetName(Guid id)
            {
                var result = (from c in m_db.aspnet_Memberships
                              where c.UserId == id
                              select c.FullName).SingleOrDefault();
                return (result);
            }

            public aspnet_User GetUserById(Guid id)
            {
                var result = (from u in m_db.aspnet_Users
                              where u.UserId == id
                              select u).SingleOrDefault();
                return result;
            }

           public IEnumerable<aspnet_Membership> GetAllProfessionals()
           { 
                var professional = new Guid("3393e07b-717c-4c95-8ee5-1e7082345ced");
                var result = from u in( from userRoles in m_db.aspnet_UsersInRoles
                                        where userRoles.RoleId == professional
                                        select userRoles)
                             from m in m_db.aspnet_Memberships
                             where u.UserId == m.UserId
                             select m;
                return result;
           }

           public IEnumerable<aspnet_Role> GetAllRoles()
           {
               var result = from roles in m_db.aspnet_Roles
                            select roles;
               return result;
           }

           public IEnumerable<aspnet_Membership> GetAllRoleRequests()
           {
               var result = from r in m_db.aspnet_Memberships
                            where r.fagadili == true
                            select r;
               return result;
           }

           public void UpdateUserInfo( aspnet_Membership userToUpdate)
           {
               var user = (from userInfo in m_db.aspnet_Memberships
                           where userInfo.UserId == userToUpdate.UserId
                           select userInfo).SingleOrDefault();
               user = userToUpdate;
               m_db.SubmitChanges();
           }

           public aspnet_Membership GetUserInfoById(Guid id)
           {
               var result =   (from userInfo in m_db.aspnet_Memberships
                               where userInfo.UserId == id
                               select userInfo).SingleOrDefault();
               return result;
           }


        #endregion
       
        #region Children
           public void SaveChild(Child childToSave)
           {
               if (childToSave != null)
               {
                   m_db.Childs.InsertOnSubmit(childToSave); 
                   m_db.SubmitChanges();
               }

           }
           public IEnumerable<Child> GetAllChildren()
           {
               var result = from child in m_db.Childs
                            orderby child.Name
                            select child;
               return (result);
           }

           public void UpdateChild(int id, Child changedChild)
           {
               var toChange = (from c in m_db.Childs
                               where c.ID == id
                               select c).SingleOrDefault();
               toChange.Name = changedChild.Name;
               toChange.Dateofbirth = changedChild.Dateofbirth;
               m_db.SubmitChanges();
           }
           public Child GetChildByID(int? id)
           {
               var result = (from c in m_db.Childs
                             where c.ID == id
                             select c).SingleOrDefault();
               return (result);
           }

           public IEnumerable<Child> GetChildByParentid(Guid parentid)
           {
               var result = from parent in m_db.Childs
                            where parent.ParentID == parentid
                            select parent;
               return (result);
           }

        #endregion children

        #region Surveys
           public void SaveSurvey(Survey surveyToSave)
           {
               if (surveyToSave != null)
               {
                   m_db.Surveys.InsertOnSubmit(surveyToSave);
                   m_db.SubmitChanges();
               }

           }
           public Survey GetNewestSurvey()
           {
               var result = (from c in m_db.Surveys
                             orderby c.DateAdded descending
                             select c).FirstOrDefault();
               return (result);
           }
        #endregion Surveys

    }
}